package com.wang.other_test.ThreadTest;

import com.wang.other_test.test.Cat;

import java.util.Set;
import java.util.concurrent.Callable;

/**
 * @author: lvyan
 * @date: 2023/1/31
 * @description:
 */
public class ThreadTest implements Callable<Set<Cat>> {
    private Set<Cat> catSet;

    public ThreadTest(Set<Cat> catSet) {
        this.catSet = catSet;
    }

    @Override
    public Set<Cat> call() throws Exception {
        //模拟执行 随机数*1000 秒
        Thread.sleep((long) (Math.random()*1000));
        for (int i = 0; i < 2; i++) {
            Cat cat = new Cat();
            cat.setName("ThreadCat"+i);
            cat.setAge((int) ((Math.random()*100)+Math.random()*100));
            cat.setAnswer(null);
            catSet.add(cat);
        }
        Cat cat2 = new Cat();
        cat2.setName("ThreadCat");
        cat2.setAge(1);
        cat2.setAnswer(null);

        if (!catSet.contains(cat2)) {
            catSet.add(cat2);
        }else{
            for (int i = 1; i < 5; i++) {
                cat2.setAge(cat2.getAge() + 1);
                if (!catSet.contains(cat2)){
                    catSet.add(cat2);
                }
            }
        }
        return catSet;
    }

}
